<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                messageRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#messageGird").jqGrid('getGridParam', 'selrow');
                        var message = jQuery("#messageGird").jqGrid('getRowData', rowid);
                        dijit.byId('messageUFD').setValues(message);
                    }
                };
                topic.subscribe("message/insertOrUpdate", messageRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#messageGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#messageGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#messageGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#messageGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                messageSubmitForm = function(formId, type) {
                    var url = "message/insert";
                    if (type == 'u') {
                        url = "message/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues());
                        syncDB(url, json);
                        form.hide();
                        topic.publish("message/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                messageDelete = function() {
                    var ids = jQuery("#messageGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var message = {};
                            message.id = '';
                            array.forEach(ids, function(entry, i) {
                                message.id += entry + ",";
                            });
                            syncDB("message/delete", JSON.stringify(message));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                messageQuery = function(formId) {
                    var message = dijit.byId(formId).getValues();
                    jQuery("#messageGird").jqGrid('setGridParam', {
                        postData : message,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryMessageForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="xxlx">消息类型:</label></td>
          <td>
              <select data-dojo-type="dijit/form/Select" name="xxlx">
                 <option>全部</option>
                 <option value="101">拟稿</option>
                 <option value="102">核稿</option>
                 <option value="103">会签</option>
                 <option value="104">签发</option>
                 <option value="105">成文</option>
                 <option value="106">分发</option>
                 <option value="107">归档</option>
                 <option value="201">登记</option>
                 <option value="202">拟办</option>
                 <option value="203">批办</option>
                 <option value="204">承办</option>
                 <option value="205">协办</option>
                 <option value="206">传阅</option>
                 <option value="207">催办</option>
                 <option value="208">办复</option>
                 <option value="209">归档</option>
              </select>
          </td>
          <td><label for="xxfsrxm">发送人: </label></td>
          <td>
              <input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="xxfsrxm" onclick="showUserTree(this,'messagexxfsrid');" />
              <input  type="hidden" id="messagexxfsrid" />
          </td>
        </tr>        
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="messageDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="messageDelete();"></button>
    <button id="messageQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="messageQuery('queryMessageForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="messageGird"></table>
  <div id="messageToolbar"></div>
</div>
<script type="text/javascript">
    var xxztmap={
            1 : "已读",
            0 : "未读"
    }
    
    var xxlxmap={
            101 : "拟稿",
            102 : "核稿",
            103 : "会签",
            104 : "签发",
            105 : "成文",
            106 : "分发",
            107 : "归档",
            201 : "登记",
            202 : "拟办",
            203 : "批办",
            204 : "承办",
            205 : "协办",
            206 : "传阅",
            207 : "催办",
            208 : "办复",
            209 : "归档"
    }
    jQuery("#messageGird").jqGrid({
        url : 'message/queryAsList?all=1',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '消息ID',
          '消息类型:',
          '消息内容',
          '消息发送人姓名',
          '消息发送人ID',
          '消息接收人姓名',
          '消息接收人用户ID',
          '接收单位代码',
          '接收单位名称',
          '消息状态',
          '消息阅读时间',
          '删除标识：0未删除（默认值）、1已删除',
          '删除人',
          '删除人ID',
          '删除单位代码',
          '删除单位名称',
          '删除时间'
        ],
        colModel : [ 
        {
          name : 'xxid',
          index : 'xxid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'xxlx',
          align : 'center',
          index : 'xxlx'
        },
        {
          name : 'xxnr',
          index : 'xxnr'
        },
        {
          name : 'xxfsrxm',
          align : 'center',
          index : 'xxfsrxm'
        },
        {
          name : 'xxfsrid',
          hidden : true,
          index : 'xxfsrid'
        },
        {
          name : 'xxjsrxm',
          align : 'center',
          index : 'xxjsrxm'
        },
        {
          name : 'xxjsrid',
          hidden : true,
          index : 'xxjsrid'
        },
        {
          name : 'xxjsdwdm',
          hidden : true,
          index : 'xxjsdwdm'
        },
        {
          name : 'xxjsdwmc',
          hidden : true,
          index : 'xxjsdwmc'
        },
        {
          name : 'xxzt',
          align : 'center',
          index : 'xxzt'
        },
        {
          name : 'xxydsj',
          align : 'center',
          index : 'xxydsj'
        },
        {
          name : 'scbs',
          hidden : true,
          index : 'scbs'
        },
        {
          name : 'scr',
          hidden : true,
          index : 'scr'
        },
        {
          name : 'scrid',
          hidden : true,
          index : 'scrid'
        },
        {
          name : 'scdwdm',
          hidden : true,
          index : 'scdwdm'
        },
        {
          name : 'scdwmc',
          hidden : true,
          index : 'scdwmc'
        },
        {
          name : 'scsj',
          hidden : true,
          index : 'scsj'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#messageToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        gridComplete : function() {
            var ids = jQuery("#messageGird").jqGrid('getDataIDs');
            for(var i = 0;i < ids.length;i++){
                var cl = ids[i];
                var nowrow = jQuery("#messageGird").jqGrid('getRowData', cl);
                var myxxzt=xxztmap[nowrow["xxzt"]];
                var myxxlx=xxlxmap[nowrow["xxlx"]];
                jQuery("#messageGird").jqGrid('setRowData', cl, {
                    xxzt : myxxzt,
                    xxlx : myxxlx
                });
            }
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "消息表列表"
    });
    jQuery("#messageGird").jqGrid('navGrid', '#messageToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
